package com.john.blog.common.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.john.blog.common.vo.FtpConfig;

public class SftpUtil {

    private static transient Logger log = LoggerFactory.getLogger(SftpUtil.class);

    private static String UTF8 = "UTF-8";

    /**
     * 获取sftp连接
     *
     * @param ftpConfig
     * @return
     * @throws JSchException
     */
    private static ChannelSftp getChannel(FtpConfig ftpConfig) throws Exception {
        String host = ftpConfig.getIp();
        int port = ftpConfig.getPort();
        String username = ftpConfig.getUsername();
        JSch jsch = new JSch();
        Session session = jsch.getSession(username, host, port);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.setPassword(ftpConfig.getPassword());
        session.connect();
        ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
        channel.connect();
        channel.setFilenameEncoding(UTF8);
        return channel;
    }

    /**
     * 关闭channel
     *
     * @param channel
     */
    private static void close(Channel channel) throws Exception {
        Session session = null;
        if (channel != null) {
            session = channel.getSession();
            channel.disconnect();
            session.disconnect();
        }
    }

    /**
     * 文件上传
     *
     * @param file
     * @return
     * @throws IOException
     * @throws SftpException
     * @throws JSchException
     */
    public static boolean uploadFile(FtpConfig ftpConfig, String fileName, InputStream inputStream) {
        ChannelSftp channel = null;
        try {
            channel = getChannel(ftpConfig);
            channel._put(inputStream, ftpConfig.getDefaultRemoteDir() + "/" + fileName, null, 0);
            close(channel);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        }
    }

    /**
     * 通过文件name删除文件
     * 
     * @param ftpConfig
     * @param fileName
     * @return
     */
    public static boolean deleteFile(FtpConfig ftpConfig, String fileName) {
        ChannelSftp channel = null;
        try {
            channel = getChannel(ftpConfig);
            channel.cd(ftpConfig.getDefaultRemoteDir());
            @SuppressWarnings("rawtypes")
            Vector content = channel.ls(fileName);
            if (content != null) {
                channel.rm(fileName);
            }
            close(channel);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        }
    }

}
